#include <zenilib.h>
#include "Person.h"
#include "Globals.h"

Person::Person(Vector2 pos_) :
	SpriteObject(pos_, Vector2(75, 150))
{
	addTexture("PersonStillShirt");
	addTexture("PersonStill");
	preTexture = "Person";
	poly = Poly(poly[0] + Vector2(39.0f * scale.x / 128.0f, 0), poly[2] - Vector2(34.0f * scale.x / 128.0f, 0));
	hostHealth = maxHostHealth = 80;
}

void Person::update(const float &timestep)
{
	if (!dead()){
		if (textures[1] != "Boss1Attack"){
			const float maxElapsed = 0.125f;
			if (grounded()){
				if (textures[1] == "Boss1Jump"){
					setRot(0);
					setScale(Vector2(100, 200));
					rotvel = 0;
				}
				if (!moving){
					textures[1] = preTexture + "Still";
					textureNum = 0;
					textureChangeElapsedTime = maxElapsed;
				}
				else{
					textureChangeElapsedTime += timestep;
					if (textureChangeElapsedTime > maxElapsed){
						textureChangeElapsedTime = 0;
						textureNum = (textureNum + 1) % 4;
						switch (textureNum){
							case 1:
								textures[1] = preTexture + "Walk1";
								break;
							case 3:
								textures[1] = preTexture + "Walk2";
								break;
							default:
								textures[1] = preTexture + "Still";
								break;
						}
					}
				}
			}
			else{
				textures[1] = preTexture + "Jump";
				if (preTexture == "Boss1"){
					setScale(Vector2(100, 100));
					rotvel = flipped ? 4 * pi : -4 * pi;
				}
				textureNum = 0;
				textureChangeElapsedTime = maxElapsed;
			}
		}
	}
	else{
		if (!(deadTime > 0) && preTexture == "Boss1"){
			setRot(0);
			setScale(Vector2(100, 200));
			rotvel = 0;
		}
		textures[1] = preTexture + "Still";
	
		vel.x = 0;
		if (rot <= -pi / 2){
			setRot(-pi / 2);
			rotvel = 0;
		}
		else{
			rotvel = -pi / 2;
		}
		deadTime += timestep;
		if (deadTime > 4.0f){
			gone = true;
		}
	}
	if (preTexture != "Agent" && preTexture != "Boss1"){
		textures[0] = textures[1] + "Shirt";
	}
	GameObject::update(timestep);
}

void Person::renderExtras()
{
	if (preTexture == "Agent" || preTexture == "PersonWeapon"){
		render_image(preTexture + "Arm",
					 (pos - scale / 2).toPoint2f(),
					 (pos + scale / 2).toPoint2f(),
					 rot,
					 1,
					 pos.toPoint2f(),
					 flipped);
	}
}